/*
* @(#)ShortDocumentNameConvertor.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.core;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.Crypt;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;

/**
 * <p>Class Name:       ShortDocumentNameConvertor.java    </p>
 * <p>Description:      This class is providing an the method for all kind of method on Document name convertion.</p>
 * @author              Zoe Shum
 * @company             DCIVision Limited
 * @creation date       08/08/2003
 * @version             $Revision: 1.3 $
 */

public class ShortDocumentNameConvertor implements DmsDocumentNameConvertor {

  public static final String REVISION = "$Revision: 1.3 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());

  
  /**
  * Encrypt the dmsDocument name
  *
  * @param      dmsDocument   the dmsDocument object 
  * @return     Encrypt document name
  * @throws ApplicationException
  */
  public String encryptDocumentName (DmsDocument dmsDocument) throws ApplicationException{
    String tmpName = dmsDocument.getID().intValue() +"";
    int stringLeng = 10;
    if (tmpName!=null) {
      if(tmpName.length() < 10){
        for(int i=tmpName.length();i< stringLeng ;i++){
          tmpName = "0"+tmpName;
        }
      }
    }    
    return(Crypt.encrypt(tmpName, SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT)));
    //return tmpName;
  }

  /* Input the dmsDocument and decrypt the dmsDocument name */
  /**
    * Decrypt the encrypted dmsDocument name and the result is put into the hashtable.
    * <Pre>
    *
    * </Pre>
    *
    * @param      encryptedName   the dmsDocument name 
    * @return     HashMap of the decrypted result
    * @throws ApplicationException
    */
  public HashMap decryptDocumentName (String encryptedName) throws ApplicationException{
    HashMap hMap = new HashMap();
    String realStr = Crypt.decrypt(encryptedName, SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT));
//    Integer rootID = TextUtility.parseIntegerObj(realStr.substring(realStr.length()-6, realStr.length()));
//    Integer parentID = TextUtility.parseIntegerObj(realStr.substring(realStr.length()-13, realStr.length()-7));
//    String verNo = realStr.substring(realStr.length()-20, realStr.length()-14);
//    Integer docID = TextUtility.parseIntegerObj(realStr.substring(realStr.length()-28, realStr.length()-21));
//    String documentName = realStr.substring(0, realStr.length()-29);
//    hMap.put("rootID", rootID);
//    hMap.put("parentID", parentID);
//    hMap.put("versionNo", verNo);
//    hMap.put("documentID", docID);
//    hMap.put("documentName", documentName);
    /*log.debug("rootID = " + rootID);
    log.debug("parentID = " + parentID);
    log.debug("versionNo = " + verNo);
    log.debug("documentID = " + docID);
    log.debug("documentName = " + documentName);*/
    return(hMap);
  }
}